package com.seo.service.keyword.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.seo.mapper.keyword.KeywordMapper;
import com.seo.mapper.user.ConfigMapper;
import com.seo.pojo.ReturnBody;
import com.seo.service.keyword.sdk.KeywordRevisionService;
import com.seo.service.user.sdk.UserService;
import com.seo.utils.CommonUtil;
import com.seo.utils.KeyWordsIndex;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service("keywordRevisionService")
public class KeywordRevisionServiceImpl implements KeywordRevisionService {

	private Map<String, KeywordRevisionServiceImpl> keywordServiceMap = new HashMap<>();

	@Autowired
	private KeywordMapper keywordMapper;

	@Autowired
	private UserService userService;
	
	@Autowired
	private ConfigMapper configMapper;

	@Value("${keyword.key}")
	private String key;
	@Value("${keyword.shenma.url}")
	private String shenmaUrl;
	@Value("${keyword.so.url}")
	private String soUrl;
	@Value("${keyword.sougou.url}")
	private String sougouUrl;
	@Value("${keyword.baidu.url}")
	private String baiduUrl;

	@PostConstruct
	public void init() {
		log.info("交叉上传注册[{}]", "01");
		keywordServiceMap.put("01", this);
	}


	@Override
	public List<Map<String, Object>> getEngineList() {
		return keywordMapper.getEngineList();
	}


	@SuppressWarnings("unchecked")
	@Override
	public ReturnBody saveKeyWordToAdmin(Map<String, Object> param) {
		log.info("admin添加关键词开始--- param={}", param);
		ReturnBody rb = ReturnBody.init();
		String uploadType = param.get("uploadType") + "";
		String userName = param.get("userName") + "";
		ReturnBody userByName = userService.getUserByName(userName);
		if (!userByName.isSuccess()) {
			return rb.rollback("用户不存在");
		} 
		Map<String, Object> user = (Map<String, Object>) userByName.get("user");
		String parentId = user.get("parentId")+"";
		if(!"0".equals(parentId)) {
			return rb.rollback("此用户不是本账号下级用户");
		}
		param.put("user", user);
		param.put("isLowerLevel", "1");
		try {
			if("01".equals(uploadType)) {
				//交叉上传
				rb.run(saveCrossKeyWord(param, "01"));
			} else if("02".equals(uploadType)) {
				//批量上传
				rb.run(saveBatchKeyWord(param,"01"));
			}
		} catch (Exception e) {
			log.info("admin添加关键词异常--- e={}", e);
			return rb.rollback("关键词添加失败");
		} finally {
			rb.result();
		}
		return rb;
	}
	
	/**
	 * 批量上传关键词
	 * @return ReturnBody
	 */
	@SuppressWarnings("unchecked")
	public ReturnBody saveBatchKeyWord(Map<String, Object> param, String serviceType) {
		log.info("批量上传关键词开始--- param={}", param);
		ReturnBody rb = ReturnBody.init();
		String urlKeyword = param.get("urlKeyword") + "";
		Map<String, Object> user = (Map<String, Object>) param.get("user");
		String createId = param.get("createId") + "";
		String engineCode = param.get("engineCode") + "";
		String isLowerLevel = param.get("isLowerLevel") + "";
		engineCode = engineCode.substring(0, engineCode.length() - 1);
		if (StringUtils.isEmpty(urlKeyword) || user == null || StringUtils.isEmpty(createId)
				|| StringUtils.isEmpty(engineCode)) {
			return rb.rollback("参数不能为空");
		}

		rb.run(CommonUtil.isUrlKeywordGroup(urlKeyword));
		
		if (!rb.isSuccess()) {
			rb.getResult().setIsRollBack("01");
			return rb;
		}

		String[] engineCodes = engineCode.split(",");

		List<String> domains = rb.getCollection("urlList", String.class);
		List<String> keywordList = rb.getCollection("keywordList", String.class);
		try {
			//获取用户子父信息
			log.info("获取用户子父信息开始--- userId={}", user.get("id"));
			List<Map<String,Object>> userParent = getUserParent(user);
			if(userParent == null || userParent.size() == 0) {
				return rb.rollback("用户不存在");
			}
			log.info("获取用户子父信息结束--- userRelation={}", userParent);
			List<Map<String, Object>> successList = new ArrayList<>();
			List<Map<String, Object>> failList = new ArrayList<>();
			for (int i = 0; i < engineCodes.length; i++) {
				for (int j = 0; j < domains.size(); j++) {
					//封装关键词字段
					Map<String, Object> pa = new HashMap<>();
					String kw = keywordList.get(j).trim();
					String url = domains.get(j);
					pa.put("userId", userParent.get(0).get("id"));
					pa.put("realUserId", userParent.get(userParent.size()-1).get("id"));
					pa.put("keyWord", kw);
					pa.put("engineCode", engineCodes[i]);
					pa.put("engineCodes", getEngineCode(engineCodes[i]));
					pa.put("domain", url);
					pa.put("createId", createId);
					pa.put("state", "01");
					int count = keywordMapper.getKeyWordNum(pa);
					if(count > 0) {
						failList.add(pa);
						continue;
					}
					String index = KeyWordsIndex.getKeyWordsInde(kw, key, engineCodes[i]);
					pa.put("keywordIndex", index);
					pa.put("initRanking", KeyWordsIndex.getKeyWordsRanking(kw, url, engineCodes[i]).get("ranking"));
					pa.put("domainType", "http://");
					Map<String,Object> vipLevel = null;
					//获取顶级用户信息
					Map<String,Object> ma = userParent.get(0);
					
					String state = "";
					String price = "";
					if(ma != null) {
						int msg = getUserState(ma, user,isLowerLevel);
						if(msg == 0) {
							return rb.rollback("添加失败，余额过低或用户异常");
						}
						String price1 = ma.get("price1")+"";
						if(!"null".equals(price1) && !"".equals(price1) && !"0.0000".equals(price1)) {
							price = price1;
						} else {
						
							String roleCode =  ma.get("roleCode")+"";
							if("proxy".equals(roleCode) || "02".equals(roleCode)) {
								ma.put("roleCode", "02");
							} else {
								ma.put("roleCode", "01");
							}
							ma.put("serviceType", serviceType);
							ma.put("types", "kp");
							//获取顶级用户价格信息
							String parentId = ma.get("parent_id")+"";
							if("0".equals(parentId)) {
								vipLevel = userService.getPriceToVipLevel(ma);
							} else {
								ma.put("superId", parentId);
								vipLevel = userService.getUserPriceToVipLevel(ma);
							}
							if(vipLevel == null || vipLevel.isEmpty()) {
								return rb.rollback("未查找到用户价格");
							}
							state = (String)vipLevel.get("state");
							if(!"01".equals(state)) {
								return rb.rollback("用户等级价格未启用");
							}
							String init_price = vipLevel.get("init_price")+"";
							if("0.0000".equals(init_price) || "0".equals(init_price) || "".equals(init_price) || init_price == null) {
								return rb.rollback("用户等级价格未设置");
							}
							price = getPriceMethod(vipLevel, index);
						}
					}
					//获取价格
					pa.put("price", price);
					//添加关键词
					keywordMapper.saveKeyWordRevision(pa);
					successList.add(pa);
					int x = 0;
					String relationId = "";
					//添加关键词关联表
					for (Map<String, Object> map : userParent) {
						Map<String,Object> m = new HashMap<>();
						Map<String,Object> pvl = null;
						if(map != null) {
							int msg = getUserState(map,user,isLowerLevel);
							if(msg == 0) {
								return rb.rollback("添加失败，余额过低或用户异常");
							}
							String price1 = map.get("price1")+"";
							if(!"null".equals(price1) && !"".equals(price1) && !"0.0000".equals(price1)) {
								price = price1;
							} else {
								String roleCode =  map.get("roleCode")+"";
								if("proxy".equals(roleCode) || "02".equals(roleCode)) {
									map.put("roleCode", "02");
								} else {
									map.put("roleCode", "01");
								}
								map.put("serviceType", serviceType);
								map.put("types", "kp");
								String parentId = map.get("parent_id")+"";
								if("0".equals(parentId)) {
									pvl = userService.getPriceToVipLevel(map);
								} else {
									map.put("superId", parentId);
									pvl = userService.getUserPriceToVipLevel(map);
								}
								if(pvl == null || pvl.isEmpty()) {
									return rb.rollback("未查找到用户价格");
								}
								state = (String)pvl.get("state");
								if(!"01".equals(state)) {
									return rb.rollback("用户等级价格未启用");
								}
								String init_price = pvl.get("init_price")+"";
								if("0.0000".equals(init_price) || "0".equals(init_price) || "".equals(init_price) || init_price == null) {
									return rb.rollback("用户等级价格未设置");
								}
								price = getPriceMethod(pvl, index);
							}
						}
					
						m.put("keywords_id", pa.get("id"));
						m.put("parentId", map.get("parent_id"));
						m.put("userId", map.get("id"));
						m.put("price", price);
						m.put("create_id", createId);
						//添加关键词关联表
						keywordMapper.saveKeyWordRelation(m);
						if(x != 0) {
							m.put("relationId", relationId);
							//修改关键词关联表下级用户
							keywordMapper.updateKeyWordRelationToChildUserId(m);
							relationId = m.get("id")+"";
						} else {
							relationId = m.get("id")+"";
						}
						x++;
					}
				}
			}
			rb.put("successList", successList);
			rb.put("failList", failList);
		} catch (Exception e) {
			log.info("批量上传关键词开始--- e={}", e);
			return rb.rollback("关键词添加失败");
		} finally {
			rb.result();
		}
		return rb.success();
	}


	/**
	 * 交叉上传关键词
	 * @return ReturnBody
	 */
	@SuppressWarnings("unchecked")
	public ReturnBody saveCrossKeyWord(Map<String, Object> param, String serviceType) {
		log.info("交叉上传关键词开始--- param={}", param);
		ReturnBody rb = ReturnBody.init();
		try {
			Map<String, Object> user = (Map<String, Object>) param.get("user");
			String keyWord = param.get("keyWord") + "";
			String domain = param.get("domain") + "";
			String createId = param.get("createId") + "";
			String engineCode = param.get("engineCode") + "";
			String isLowerLevel = param.get("isLowerLevel") + "";
			engineCode = engineCode.substring(0, engineCode.length() - 1);
			if (StringUtils.isEmpty(keyWord) || StringUtils.isEmpty(domain) || user == null
					|| StringUtils.isEmpty(createId) || StringUtils.isEmpty(engineCode)) {

				return rb.rollback("参数不能为空");
			}

			rb.run(CommonUtil.isUrlGroup(domain));

			if (!rb.isSuccess()) {
				rb.getResult().setIsRollBack("01");
				return rb;
			}
			rb.run(CommonUtil.isKeywordGroup(keyWord, "\n"));
			if (!rb.isSuccess()) {
				rb.getResult().setIsRollBack("01");
				return rb;
			}
			List<String> domains = rb.getCollection("urlList", String.class);
			String[] engineCodes = engineCode.split(",");
			List<String> keyWords = rb.getCollection("keywordList", String.class);
			//获取用户子父信息
			log.info("获取用户子父信息开始--- userId={}", user.get("id"));
			List<Map<String,Object>> userParent = getUserParent(user);
			if(userParent == null || userParent.size() == 0) {
				return rb.rollback("用户不存在");
			}
			log.info("获取用户子父信息结束--- userRelation={}", userParent);
			List<Map<String, Object>> successList = new ArrayList<>();
			List<Map<String, Object>> failList = new ArrayList<>();
			for (int i = 0; i < engineCodes.length; i++) {
				for (int j = 0; j < domains.size(); j++) {
					for (int y = 0; y < keyWords.size(); y++) {
						String kw = keyWords.get(y).trim();
						//封装关键词字段
						Map<String, Object> pa = new HashMap<>();
						pa.put("userId", userParent.get(0).get("id"));
						pa.put("realUserId", userParent.get(userParent.size()-1).get("id"));
						pa.put("keyWord", kw);
						pa.put("createId", createId);
						pa.put("engineCode", engineCodes[i]);
						pa.put("engineCodes", getEngineCode(engineCodes[i]));
						pa.put("domain", domains.get(j));
						pa.put("state", "01");
						int count = keywordMapper.getKeyWordNum(pa);
						if(count > 0) {
							failList.add(pa);
							continue;
						}
						String index = KeyWordsIndex.getKeyWordsInde(kw, key, engineCodes[i]);
						pa.put("keywordIndex", index);
						pa.put("initRanking", KeyWordsIndex.getKeyWordsRanking(kw, domains.get(j), engineCodes[i]).get("ranking"));
						pa.put("domainType", "http://");
						
						Map<String,Object> vipLevel = null;
							//获取顶级用户信息
						Map<String,Object> ma = userParent.get(0);
						
						String state = "";
						String price = "";
						if(ma != null) {
							int msg = getUserState(ma,user,isLowerLevel);
							if(msg == 0) {
								return rb.rollback("添加失败，余额过低或用户异常");
							}
							String price1 = ma.get("price1")+"";
							if(!"null".equals(price1) && !"".equals(price1) && !"0.0000".equals(price1)) {
									price = price1;
							} else {
								
								String roleCode =  ma.get("roleCode")+"";
								if("proxy".equals(roleCode) || "02".equals(roleCode)) {
									ma.put("roleCode", "02");
								} else {
									ma.put("roleCode", "01");
								}
								ma.put("serviceType", serviceType);
								ma.put("types", "kp");
								//获取顶级用户价格信息
								String parentId = ma.get("parent_id")+"";
								if("0".equals(parentId)) {
									vipLevel = userService.getPriceToVipLevel(ma);
								} else {
									ma.put("superId", parentId);
									vipLevel = userService.getUserPriceToVipLevel(ma);
								}
								if(vipLevel == null || vipLevel.isEmpty()) {
									return rb.rollback("未查找到用户价格");
								}
								state = (String)vipLevel.get("state");
								if(!"01".equals(state)) {
									return rb.rollback("用户等级价格未启用");
								}
								String init_price = vipLevel.get("init_price")+"";
								if("0.0000".equals(init_price) || "0".equals(init_price) || "".equals(init_price) || init_price == null) {
									return rb.rollback("用户等级价格未设置");
								}
								price = getPriceMethod(vipLevel, index);
							}
						}
						
						pa.put("price", price);
						//添加关键词
						keywordMapper.saveKeyWordRevision(pa);
						successList.add(pa);
						int x = 0;
						String relationId = "";
						//添加关键词关联表
						for (Map<String, Object> map : userParent) {
							Map<String,Object> m = new HashMap<>();
							Map<String,Object> pvl = null;
							if(map != null) {
								int msg = getUserState(map,user,isLowerLevel);
								if(msg == 0) {
									return rb.rollback("添加失败，余额过低或用户异常");
								}
								String price1 = map.get("price1")+"";
								if(!"null".equals(price1) && !"".equals(price1) && !"0.0000".equals(price1)) {
									price = price1;
								} else {
									String roleCode =  map.get("roleCode")+"";
									if("proxy".equals(roleCode) || "02".equals(roleCode)) {
										map.put("roleCode", "02");
									} else {
										map.put("roleCode", "01");
									}
									map.put("serviceType", serviceType);
									map.put("types", "kp");
									String parentId = map.get("parent_id")+"";
									if("0".equals(parentId)) {
										pvl = userService.getPriceToVipLevel(map);
									} else {
										map.put("superId", parentId);
										pvl = userService.getUserPriceToVipLevel(map);
									}
									if(pvl == null || pvl.isEmpty()) {
										return rb.rollback("未查找到用户价格");
									}
									state = (String)pvl.get("state");
									if(!"01".equals(state)) {
										return rb.rollback("用户等级价格未启用");
									}
									String init_price = pvl.get("init_price")+"";
									if("0.0000".equals(init_price) || "0".equals(init_price) || "".equals(init_price) || init_price == null) {
										return rb.rollback("用户等级价格未设置");
									}
									price = getPriceMethod(pvl, index);
								}
							}
							m.put("keywords_id", pa.get("id"));
							m.put("parentId", map.get("parent_id"));
							m.put("userId", map.get("id"));
							m.put("price", price);
							m.put("create_id", createId);
							//添加关键词价格关联表
							keywordMapper.saveKeyWordRelation(m);
							if(x != 0) {
								m.put("relationId", relationId);
								//修改关键词关联表下级用户
								keywordMapper.updateKeyWordRelationToChildUserId(m);
								relationId = m.get("id")+"";
							} else {
								relationId = m.get("id")+"";
							}
							x++;
						}
					}
				}
			}
			rb.put("successList", successList);
			rb.put("failList", failList);
		} catch (Exception e) {
			log.info("交叉上传关键词开始--- e={}", e);
			return rb.rollback("关键词添加失败");
		} finally {
			rb.result();
		}
		return rb.success();
	}

	private int getUserState(Map<String, Object> ma, Map<String, Object> user,String isll) {
		int num = 0;
		int uid = (Integer) user.get("id");
		int id = (Integer) ma.get("id");
		String state = (String) ma.get("state");
		String isDelete = (String) ma.get("isDelete");
		BigDecimal balance = (BigDecimal) ma.get("balance");
		BigDecimal minQuota = (BigDecimal) ma.get("minQuota");
		if(minQuota.compareTo(BigDecimal.ZERO) < 0) {
			String quota = configMapper.getConfig("min_quota");
			minQuota = new BigDecimal(quota);
		}
		
		if("02".equals(state) || "01".equals(isDelete)) {
			return num;
		}
		if(!"1".equals(isll)){
			if(balance == null || minQuota == null) {
				return num;
			}
			if(balance.compareTo(minQuota) == -1) {
				return num;
			}
		} else {
			log.info("上级用户[{}]当前用户[{}]", id ,uid);
			if(id != uid) {
				if(balance == null || minQuota == null) {
					return num;
				}
				if(balance.compareTo(minQuota) == -1) {
					return num;
				}
			}
		}
	
		num = 1;
		return num;
	}


	public String getPriceMethod(Map<String, Object> vipLevel, String index) {
		BigDecimal price = null;
		//原始价格
		BigDecimal init_price = (BigDecimal) vipLevel.get("init_price");
		//指数递增价格
		BigDecimal add_price = (BigDecimal) vipLevel.get("add_price");
		//递增指数
		Integer add_index = (Integer) vipLevel.get("add_index");
		//关键词指数
		Integer indexs = Integer.parseInt(index);
		if(indexs == -1) {
			indexs = 0;
		}
		if(add_index == 0) {
			return init_price+"";
		}
		Integer indexUnit = 0;
		if(indexs > add_index) {
			indexUnit = indexs / add_index;
			Integer remainder = indexs % add_index;
			if(remainder > 0) {
				indexUnit = indexUnit+1;
			}
		}
		BigDecimal addi = new BigDecimal(indexUnit.toString());
		BigDecimal price1 = addi.multiply(add_price);
		price = price1.add(init_price);
		return price+"";
	}

	public String getEngineCode(String engineCodes) {
		if ("01".equals(engineCodes)) {
			engineCodes = "百度PC";
		}
		if ("02".equals(engineCodes)) {
			engineCodes = "百度移动";
		}
		if ("03".equals(engineCodes)) {
			engineCodes = "360PC";
		}
		if ("04".equals(engineCodes)) {
			engineCodes = "360移动";
		}
		if ("05".equals(engineCodes)) {
			engineCodes = "神马";
		}
		if ("06".equals(engineCodes)) {
			engineCodes = "搜狗pc";
		}
		if ("07".equals(engineCodes)) {
			engineCodes = "搜狗移动";
		}
		return engineCodes;
	}

	public List<Map<String,Object>> getUserParent(Map<String, Object> user) {
		List<Map<String,Object>> userRelation = userService.getUserParent(user);
		return userRelation;
	}



	@SuppressWarnings("unchecked")
	@Override
	public ReturnBody saveKeyWordToCustomer(Map<String, Object> param) {
		log.info("用户添加关键词开始--- param={}", param);
		ReturnBody rb = ReturnBody.init();
		String uploadType = param.get("uploadType") + "";
		String userName = (String) param.get("userName");
		if (!"".equals(userName) && userName != null) {
			ReturnBody userByName = userService.getUserByName(userName);
			if (!userByName.isSuccess()) {
				return rb.rollback("用户不存在");
			} 
			Map<String, Object> user = (Map<String, Object>) userByName.get("user");
			Map<String, Object> currentUser = (Map<String, Object>) param.get("currentUser");
			int parentId = (Integer)user.get("parentId");
			int id = (Integer) currentUser.get("id");
			if(parentId != id) {
				return rb.rollback("此用户不是本账号下级用户");
			}
			param.put("user", user);
			param.put("createId", 0);
			param.put("superId", parentId);
			param.put("isLowerLevel", "1");
		} else {
			Map<String, Object> currentUser = (Map<String, Object>) param.get("currentUser");
			param.put("user", currentUser);
			param.put("createId", 0);
			param.put("superId", currentUser.get("id"));
			param.put("isLowerLevel", "0");
		}
		try {
			if("01".equals(uploadType)) {
				//交叉上传     业务类型  01 优化 02 新站 03 整站
				rb.run(saveCrossKeyWord(param, "01"));
			} else if("02".equals(uploadType)) {
				//批量上传
				rb.run(saveBatchKeyWord(param, "01"));
			}
		} catch (Exception e) {
			log.info("用户添加关键词异常--- e={}", e);
			return rb.rollback("关键词添加失败");
		} finally {
			rb.result();
		}
		return rb;
	}


	@Override
	public List<Map<String, Object>> getVipLevelPriceList(Map<String, Object> param) {
		return keywordMapper.getVipLevelPriceList(param);
	}


	@Override
	public int updateVipLevelState(Map<String, Object> param) {
		return keywordMapper.updateVipLevelState(param);
	}


	/**
	 * 删除等级价格信息
	 */
	public ReturnBody deleteVipLevelById(Map<String, Object> params) {
		ReturnBody rb = ReturnBody.init();
		try {
			int num = keywordMapper.deleteVipLevelById(params);
			if (num == 0) {
				return rb.rollback();
			}
			return rb.success();
		} catch (Exception e) {
			log.error("异常-{}", e);
			return rb.rollback();
		} finally {
			rb.result();
		}
	}


	@Override
	public ReturnBody saveVipLevel(Map<String, Object> params) {
		ReturnBody rb = ReturnBody.init();
		try {
			List<Map<String, Object>> vipLevelPriceList = keywordMapper.getVipLevelPriceList(params);
			if(vipLevelPriceList !=null && vipLevelPriceList.size() > 0) {
				return rb.error("数据重复");
			}
			int num = keywordMapper.saveVipLevel(params);
			if (num == 0) {
				return rb.rollback("添加失败");
			}
		} catch (Exception e) {
			log.error("异常-{}", e);
			return rb.rollback();
		} finally {
			rb.result();
		}
		return rb.success();
	}

	@Override
	public ReturnBody updateVipLevel(Map<String, Object> params) {
		ReturnBody rb = ReturnBody.init();
		try {
			params.put("see", "01");
			List<Map<String, Object>> vipLevelPriceList = keywordMapper.getVipLevelPriceList(params);
			if (vipLevelPriceList == null || vipLevelPriceList.size() == 0) {
				return rb.error("数据不存在");
			}
			int num = keywordMapper.updateVipLevel(params);
			if (num == 0) {
				return rb.rollback("修改失败");
			}
		} catch (Exception e) {
			log.error("异常-{}", e);
			return rb.rollback();
		} finally {
			rb.result();
		}
		return rb.success();
	}

	@Override
	public int updateVipLevelIsDefault(Map<String, Object> params) {
		int num = 0;
		params.put("see", "01");
		List<Map<String, Object>> vipLevelPriceList = keywordMapper.getVipLevelPriceList(params);
		if (vipLevelPriceList == null || vipLevelPriceList.isEmpty()) {
			return num;
		}
		Map<String, Object> vlp = vipLevelPriceList.get(0);
		try {
			keywordMapper.updateVipLevelIsDefaultAll(vlp);
			num = keywordMapper.updateVipLevelIsDefault(params);
		} catch (Exception e) {
			log.error("异常-{}", e);
			return num;
		} finally {
			if (num == 0)
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}
		return num;
	}

	@Override
	public List<Map<String, Object>> getKeyWordIndex(Map<String, Object> param) {
		if (param == null || param.isEmpty()) {
			return null;
		}
		String keyWord = param.get("keyWord") + "";
		List<String> keyWords = CommonUtil.isKeywordGroup(keyWord, ",").getCollection("keywordList", String.class);
		String[] code = { "01", "02", "03", "04", "05", "06", "07" };
		List<String> params = new ArrayList<>();
		for (int i = 0; i < code.length; i++) {
			if ("01".equals(code[i]) || "02".equals(code[i])) {
				params.add(baiduUrl);
			} else if ("03".equals(code[i]) || "04".equals(code[i])) {
				params.add(soUrl);
			} else if ("05".equals(code[i])) {
				params.add(shenmaUrl);
			} else if ("06".equals(code[i]) || "07".equals(code[i])) {
				params.add(sougouUrl);
			}
		}
		List<Map<String, Object>> keyWordsIndex = KeyWordsIndex.getKeyWordsIndex(keyWords, key, params);
		return keyWordsIndex;
	}

	@Override
	public List<Map<String, Object>> getUserVipLevelPriceList(Map<String, Object> param) {
		return keywordMapper.getUserVipLevelPriceList(param);
	}

	@Override
	public int updateUserVipLevelState(Map<String, Object> param) {
		return keywordMapper.updateUserVipLevelState(param);
	}

	@Override
	public ReturnBody updateUserVipLevel(Map<String, Object> params) {
		ReturnBody rb = ReturnBody.init();
		try {
			int num = keywordMapper.updateUserVipLevel(params);
			if (num == 0) {
				return rb.rollback("修改失败");
			}
		} catch (Exception e) {
			log.error("异常-{}", e);
			return rb.rollback();
		} finally {
			rb.result();
		}
		return rb.success();
	}

	@Override
	public int updateUserVipLevelIsDefault(Map<String, Object> params) {
		int num = 0;
		params.put("see", "01");
		List<Map<String, Object>> vipLevelPriceList = keywordMapper.getUserVipLevelPriceList(params);
		if (vipLevelPriceList == null || vipLevelPriceList.isEmpty()) {
			return num;
		}
		Map<String, Object> vlp = vipLevelPriceList.get(0);
		try {
			keywordMapper.updateUserVipLevelIsDefaultAll(vlp);
			num = keywordMapper.updateUserVipLevelIsDefault(params);
		} catch (Exception e) {
			log.error("异常-{}", e);
			return num;
		} finally {
			if (num == 0)
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}
		return num;
	}

	@Override
	public int updateBacthUserVipLevelState(Map<String, Object> pam) {
		return keywordMapper.updateBacthUserVipLevelState(pam);
	}

	@Override
	public int updateBacthVipLevelState(Map<String, Object> pam) {
		return keywordMapper.updateBacthVipLevelState(pam);
	}

}